
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;
using LS.UnityExtensions.Editor;

namespace LS.GameKit.Editor
{
    [CustomEditor(typeof(CommandReceiver))]
    public class CommandReceiverInspector : InspectorBase
    {
        private List<CommandSender> m_Senders = new List<CommandSender>();
        private Dictionary<string, System.Action<CommandType, object[]>[]> m_EventModules;
        private System.Action<CommandType, object[]>[] m_CommandHandlers;

        protected override void OnEnable()
        {
            base.OnEnable();
            var interactive = target as CommandReceiver;
            var ienum = Resources.FindObjectsOfTypeAll(typeof(CommandSender))
                .OfType<CommandSender>()
                .Where(s => (s.sendChannel & CommandChannel.ReceiverObject) != 0)
                .Where(s => s.receiverObject == interactive);
            m_Senders.AddRange(ienum);

            m_EventModules = interactive.GetEventModules();
            m_CommandHandlers = interactive.GetReceiverObjectHandlers();
        }

        protected override void OnDisable()
        {
            m_Senders.Clear();
            base.OnDisable();
        }

        public override void DrawInspectorItems()
        {
            float windowWidth = EditorGUIUtility.currentViewWidth;
            GUILayout.Label("Senders");
            GUILayout.BeginVertical("box");
            using (new EditorGUI.DisabledScope(true))
            {
                var type = typeof(CommandSender);
                foreach (CommandSender sender in m_Senders)
                    EditorGUILayout.ObjectField(sender, type, true);
            }
            GUILayout.EndVertical();

            if (!EditorApplication.isPlaying) return;

            GUILayout.Label("Event Handlers");
            GUILayout.BeginVertical("box");
            using (new EditorGUI.DisabledScope(true))
            {
                foreach (var item in m_EventModules)
                {
                    GUILayout.Label(item.Key);
                    foreach (var e in item.Value)
                    {
                        GUILayout.BeginHorizontal();
                        EditorGUILayout.ObjectField(e.Target as Object, e.Target.GetType(), true,GUILayout.Width(windowWidth * 0.4f));
                        GUILayout.Label(e.Method.Name);
                        GUILayout.EndHorizontal();
                    }
                }
            }
            GUILayout.EndVertical();

            GUILayout.Label("Local Handlers");
            GUILayout.BeginVertical("box");
            using (new EditorGUI.DisabledScope(true))
            {
                foreach (var item in m_CommandHandlers)
                {
                    GUILayout.BeginHorizontal();
                    EditorGUILayout.ObjectField(item.Target as Object, item.Target.GetType(), true, GUILayout.Width(windowWidth * 0.4f));
                    GUILayout.Label(item.Method.Name);
                    GUILayout.EndHorizontal();
                }
            }
            GUILayout.EndVertical();
        }
    }
}
